﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebApplication6.DAL.DataSetProductTableAdapters;

namespace WebApplication6.manage
{
    public partial class productpic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
       

            string id = Request["id"];

            string sql = "select * from t_product where id=" + id;

            DataTable dt = SQLHelper.ExecuteDataTable(sql);

            Image1.ImageUrl = "../upload/"+ Convert.ToString(dt.Rows[0]["tupian"]);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string id = Request["id"];

            string filename = "";

            if (FileUpload1.HasFile)//用户是否选择了文件
            {
                string uploadPath = Server.MapPath("/upload/");

                //定义上传文件的格式
                string[] arrExtension = { ".jpg", ".gif" };

                //取得上传的文件名
                filename = FileUpload1.FileName;
                // 取得上传文件的扩展名 
                string strExtension = filename.Substring(filename.LastIndexOf("."));

                //判断上传文件是否合法
                int flag = 0;
                for (int i = 0; i < arrExtension.Length; i++)
                {

                    if (strExtension.Equals(arrExtension[i]))
                    {

                        flag = 1;

                    }

                }
                if (flag == 0)
                {
                    Response.Write("<script language=javascript>alert('上传失败，上传的文件必须为jpg或者gif格式');window.location.href='productpic.aspx?id=" + id + "';</script>");
                    return;
                }

                //给上传的文件重新命名,用当前时间的毫秒数命名
                DateTime t1 = new DateTime();

                t1 = Convert.ToDateTime(("2013-01-01 08:00:00"));

                string name = ((System.DateTime.Now.Ticks - t1.Ticks) / 10000).ToString();

                filename = name + strExtension;

                FileUpload1.SaveAs(uploadPath + filename);

            }


         



            string sql = "update t_product set tupian='" + filename + "'  where id=" + id;


            SQLHelper.ExecuteNonQuery(sql);

            

            Response.Write("<script language=javascript>alert('操作成功');window.location.href='productlist.aspx';</script>");
                    

        }
    }
}
